package com.flyaudio.xiaopengautopilot.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import com.flyaudio.xiaopengautopilot.R;
import com.flyaudio.xiaopengautopilot.presenter.IVehicleModule;

import java.text.DecimalFormat;

public class PowerConsumptionView implements View.OnClickListener {

    private final Context context;
    private  View v;

    private  TextView tv_total_power_consumption;
    private  TextView tv_month_power_consumption;
    private Button power_consumption_reset_bt;

    public View getView() {
        return v;
    }

    public PowerConsumptionView(Context context) {
        this.context = context;
        initView();
        update();
    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.view_power_consumption, null);
        tv_total_power_consumption = v.findViewById(R.id.tv_total_power_consumption);
        tv_month_power_consumption = v.findViewById(R.id.tv_month_power_consumption);
        power_consumption_reset_bt = v.findViewById(R.id.power_consumption_reset_bt);
        power_consumption_reset_bt.setOnClickListener(this);

    }
    private void updateTotalPowerConsumption(){
        float totalPowerAvg = IVehicleModule.getInstance().getTotalPowerAvg();
        DecimalFormat d = new DecimalFormat("0.0");
        tv_total_power_consumption.setText(d.format(totalPowerAvg));
    }
    private void updateMonthPowerConsumption(){
        float totalPowerAvg = IVehicleModule.getInstance().getMonthPowerAvg();
        DecimalFormat d = new DecimalFormat("0.0");
        tv_month_power_consumption.setText(d.format(totalPowerAvg));
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.power_consumption_reset_bt:
                ResetDialog dialog = new ResetDialog(context, R.style.setDialog, R.layout.dialog_reset);
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();
                break;
        }
    }

    public void update() {
        updateTotalPowerConsumption();
        updateMonthPowerConsumption();
    }
}
